$(function () {
    findById();
})

/**
 * 查询id
 */
function findById() {
    //得到session中的newsId
    let id = sessionStorage.getItem("newsId");
    let result = myAjax("/admin/news/findById", {id: id},"post");
    console.log(result);
    if (result != null) {
        setData(result.model);
    }

}

//赋值
function setData(data) {
    $("#typeId option[value='"+data.typeId+"']").attr("selected",true);
    $("#title").val(data.title);
    edit.txt.html(data.content);
    $("#showImg").attr("src",data.img);
    $("input[name='isShow'][value=" + data.isShow + "]").attr("checked", true);
    $("input[name='status'][value=" + data.status + "]").attr("checked", true);
    $("#publisher").val(data.publisher);

}

//返回用户查询结果页面
function goBack() {
    $('.right').load('/html/admin/news/newsManage.html');
}

//提交表单时再一次判断提交数据是否正确
function save() {
    let data = new FormData();
    data.append("id",sessionStorage.getItem("newsId"));
    data.append("typeId", $("#typeId").val());
    data.append("title", $("#title").val());
    data.append("content",edit.txt.html());
    data.append("isShow", $("input:radio[name='isShow']:checked").val());
    data.append("status", $("input:radio[name='status']:checked").val());
    data.append("publisher",$("#publisher").val());
    //拿到的图片有可能是一组数据，[0].files[0]选中当前对象的第一个数据
    data.append("img", $("#img")[0].files[0]);
    let result = myImgAjax("/adminFile/news/update", data,"post");
    console.log(result);
    if(result.result == "success"){
        alert("修改成功,返回查询界面（== ^^ ==）！")
        load("/news/newsManage.html");
    }else{
        alert("修改失败！");
    }
}